<template>
  <div class="quick-reply-wrap">
    <select-with-user-load
      ref="selectWithUserLoad"
      :loadmore-method="getQuickReplyList()"
      :add-new="true"
      :collapse-tags="false"
      :multiple="false"
      :is-can-delete="true"
      extend-class="quick-reply"
      add-new-str="添加快捷回复"
      key-name="id"
      label-name="text"
      open-tool-tip
      placeholder="请选择"
      @focus="$emit('focus')"
      @addNewClick="handleAdd"
      @onDelete="onDelete"
      @changeMethod="changeMethod"
      @visibleChange="visibleChange"
    />
    <el-dialog :visible.sync="visible" :append-to-body="true" :before-close="handleClose" class="small-common-dialog-size">
      <Title :title="'请输入要添加的快捷回复'" font-size="16px" style="margin-bottom: 24px;" />
      <el-input v-model="text" size="small" placeholder="最多不超过200字符" maxlength="200" @keyup.enter.native="addQuickReply" />
      <div style="display: flex;justify-content: center;margin-top: 12px;">
        <el-button :disabled="!text" :loading="saveLoading" type="primary" size="small" @click="addQuickReply">保存</el-button>
        <el-button size="small" @click="handleClose">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Title from '@/components/Title';
import SelectWithUserLoad from '@/components/Common/selectWithUserLoad';
import { getQuickReplyList, addQuickReply, deleteQuickReply } from '@/api/process';
export default {
  components: {
    Title,
    SelectWithUserLoad
  },
  data() {
    return {
      text: '',
      visible: false,
      saveLoading: false
    };
  },
  methods: {
    async handleClose() {
      this.text = '';
      this.visible = false;
    },
    handleAdd() {
      this.visible = true;
    },
    visibleChange(e) {
      if (!e) {
        this.getList();
      }
    },
    getList() {
      if (this.$refs.selectWithUserLoad) {
        this.$refs.selectWithUserLoad.getListData();
      }
    },
    addQuickReply() {
      this.saveLoading = true;
      addQuickReply({ text: this.text }).then(res => {
        if (res.code === 200) {
          this.$message.success('添加成功');
          this.text = '';
          this.visible = false;
          this.getList();
          this.saveLoading = false;
        }
      }).catch(err => {
        this.saveLoading = false;
        console.error(err.message);
      });
    },
    getQuickReplyList() {
      const a = function(params) {
        params.perPage = 10;
        if (params.userName) delete params.userName;
        if (params.userStatus) delete params.userStatus;
        return new Promise((resolve, reject) => {
          getQuickReplyList(params).then(res => {
            resolve(res);
          }).catch(err => {
            reject(err);
          });
        });
      };
      return a;
    },
    onDelete(id) {
      this.$confirm('是否确认删除该快捷回复?', {
        confirmButtonText: '确定',
        type: 'warning',
        showClose: false,
        center: true,
        iconClass: 'charge-icon',
        customClass: 'del-message del-message-cancel'
      }).then(() => {
        deleteQuickReply(id).then(res => {
          if (res.code === 200) {
            this.$message.success('删除成功');
            this.getList();
          }
        }).catch(err => {
          console.error(err.message);
        });
      }).catch(() => {
        return;
      });
    },
    changeMethod(obj) {
      this.$emit('selectText', obj.text);
    }
  }
};
</script>

<style lang="scss" scoped>
.quick-reply-wrap {
  &::v-deep {
    .filterableSelect {
      width: 240px;
    }
  }
}
</style>
<style>
.quick-reply {
  width: 240px !important;
}
</style>
